/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.util;

/**
 * PropertyHelper : The property helper utility class.
 * 
 * @author Romain DUBOIS
 */
public final class PropertyHelper {

    /**
     * Convert an object to a boolean value.
     * 
     * @param pValue
     *            The value
     * @param pDefault
     *            The default value (if <tt>pValue</tt> is <code>null</code>)
     * @return The boolean value
     * @throws NumberFormatException
     *             If the value is invalid
     */
    public static Boolean getBoolean(final Object pValue, final Boolean pDefault) {
        Boolean lValue = pDefault;
        if (pValue instanceof Boolean) {
            lValue = (Boolean) pValue;
        } else if (pValue != null) {
            lValue = Boolean.valueOf(pValue.toString());
        }
        return lValue;
    }

    /**
     * Convert an object to a float value.
     * 
     * @param pValue
     *            The value
     * @param pDefault
     *            The default value (if <tt>pValue</tt> is <code>null</code>)
     * @return The float value
     * @throws NumberFormatException
     *             If the value is invalid
     */
    public static Float getFloat(final Object pValue, final Float pDefault)
            throws NumberFormatException {
        Float lValue = pDefault;
        if (pValue instanceof Float) {
            lValue = (Float) pValue;
        } else if (pValue != null) {
            lValue = Float.valueOf(pValue.toString());
        }
        return lValue;
    }

    /**
     * Convert an object to an int value.
     * 
     * @param pValue
     *            The value
     * @param pDefault
     *            The default value (if <tt>pValue</tt> is <code>null</code>)
     * @return The int value
     * @throws NumberFormatException
     *             If the value is invalid
     */
    public static Integer getInt(final Object pValue, final Integer pDefault)
            throws NumberFormatException {
        Integer lValue = pDefault;
        if (pValue instanceof Integer) {
            lValue = (Integer) pValue;
        } else if (pValue != null) {
            lValue = Integer.valueOf(pValue.toString());
        }
        return lValue;
    }

    /**
     * Get a string array from a potential String.
     * 
     * @param pObject
     *            The object value
     * @return The string array
     */
    public static String[] getStringArray(final Object pObject) {
        if (pObject instanceof String[]) {
            return (String[]) pObject;
        }
        if (pObject instanceof String) {
            return new String[] { pObject.toString() };
        }
        return null;
    }

    /**
     * Constructor (private)
     */
    private PropertyHelper() {
        super();
    }
}
